package com.bizzard.org.controller;

import com.bizzard.basic.exception.BusinessException;
import com.bizzard.basic.util.AjaxResult;
import com.bizzard.basic.util.ExcelUtil;
import com.bizzard.basic.util.PageList;
import com.bizzard.org.domain.Shop;
import com.bizzard.org.domain.ShopAuditLog;
import com.bizzard.org.dto.ShopDto;
import com.bizzard.org.query.ShopQuery;
import com.bizzard.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 接口类
 */

@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺接口")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     *  查询单个对象接口
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Shop findOne (@PathVariable("id")Long id){
        return  shopService.findById(id);

    }

    /**
     *  查询所有对象接口
     * @return
     */
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Shop> findAll (){
        return  shopService.findAll();

    }

    /**
     *  分页查询接口
     * @param shopQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage (@RequestBody ShopQuery shopQuery){
        return  shopService.queryPage(shopQuery);

    }

    /**
     *  删除操作接口
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete (@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }

    }




    /**
     *  添加和修改 接口
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){//添加
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * 接口：店铺入驻
     * @param
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }


    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }


    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }

    /**
     *  导出export
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  导入importExcel
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
    }

    /**
     *  获取每一个店铺的状态
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> getShopState (){
        return  shopService.getShopState();
    }


    /**
     *  激活店铺
     */
    @GetMapping("/active/{id}")
    public  void active(@PathVariable Long id, HttpServletResponse resp){
        shopService.active(id);
        try{
            resp.sendRedirect("http://localhost:8081/#/login");
        }catch (IOException e){
            e.printStackTrace();
        }


    }



}
